package person.bing.fbcloud.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @author: mengfanbing
 * @date: 2020/12/28 11:09
 */
public class Q36 {
    public static void main(String[] args) {

    }
    public static boolean isValidSudoku(char[][] board) {
        //1.遍历每一行，有重复返回false
        //2.遍历每一列，有重复返回false
        //3.遍历每3x3方块，有重复返回false
        Set set31 = new HashSet();
        Set set32 = new HashSet();
        Set set33 = new HashSet();
        Set set34 = new HashSet();
        Set set35 = new HashSet();
        Set set36 = new HashSet();
        Set set37 = new HashSet();
        Set set38 = new HashSet();
        Set set39 = new HashSet();
        for (int i=0;i<9;i++){
            Set set = new HashSet();
            Set set2 = new HashSet();
            for (int j=0;j<9;j++){
                //判断行
                char temp = board[i][j];
                if (temp != '.'){
                    boolean add = set.add(temp);
                    if (false == add){
                        return false;
                    }
                }
                //判断列
                char temp2 = board[j][i];
                if (temp2 != '.'){
                    boolean add2 = set2.add(temp2);
                    if (false == add2){
                        return false;
                    }
                }
                //判断3x3
                if (temp != '.'){
                    boolean add = false;
                    if (i<3){
                        if (j<3){
                            add = set31.add(temp);
                        }else if (j>5){
                            add = set33.add(temp);
                        }else {
                            add = set32.add(temp);
                        }
                    }else if (i>=3 && i<=5){
                        if (j<3){
                            add = set34.add(temp);
                        }else if (j>5){
                            add = set36.add(temp);
                        }else {
                            add = set35.add(temp);
                        }
                    }else {
                        if (j<3){
                            add = set37.add(temp);
                        }else if (j>5){
                            add = set39.add(temp);
                        }else {
                            add = set38.add(temp);
                        }
                    }
                    if (false == add){
                        return false;
                    }
                }

            }
        }
        return true;
    }
}
